package fr.ensisa.hassenforder.game.server;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import javax.swing.text.html.HTMLDocument.Iterator;

import fr.ensisa.hassenforder.game.model.Player;
import fr.ensisa.hassenforder.game.model.Product;
import fr.ensisa.hassenforder.game.model.User;
import fr.ensisa.hassenforder.network.BasicAbstractWriter;
import fr.ensisa.hassenforder.network.FileHelper;
import fr.ensisa.hassenforder.network.Protocol;

public class Writer extends BasicAbstractWriter {

	public Writer(OutputStream outputStream) {
		super (outputStream);
	}
	
	public void repConnect(User u)
	{
		if(u!=null){
			writeInt(Protocol.REP_CONNECT_OK);
			writeString(u.getName());
			System.out.println(u.getId());
			writeLong(u.getId());
			
		}
		else writeInt(Protocol.REP_CONNECT_KO);
	}

	public void repDisconnect(boolean b)
	{
		writeInt(Protocol.REP_DISCONNECT);
		writeBoolean(b);
	}
	public void repStasitic(Player p)
	{
		writeInt(Protocol.REP_STRATISTIQUE);
		writeInt(p.getCash());
		writeString(p.getImage());
	}
	public void repAddCash(boolean b)
	{
		writeInt(Protocol.REP_ADD_CASH);
		writeBoolean(b);
	}
	public void repPicture(String image)
	{
		writeInt(Protocol.REP_PICTURE);
	    writeLong(FileHelper.getFileSize("./res/"+image+".png"));
	    writeString(image+".png");
	    writeBytes(FileHelper.readContent("./res/"+image+".png"));
	}
	public void repProduit(Collection<Product> p)
	{
		java.util.Iterator<Product> ip = p.iterator();
		Product pro;
		writeInt(Protocol.REP_PRODUCTS);
		writeInt(p.size());
		while(ip.hasNext())
		{
			pro = ip.next();
			writeString(pro.getCategory().name());
			writeString(pro.getName());
			writeString(pro.getImage());
			writeInt(pro.getDuration());
			writeBoolean(pro.isStackable());
			writeInt(pro.getCount());
			writeLong(pro.getTime());
		}
	}
	public void repShop(Collection<Product> p)
	{
		java.util.Iterator<Product> ip = p.iterator();
		Product pro;
		writeInt(Protocol.REP_SHOP);
		writeInt(p.size());
		while(ip.hasNext())
		{
			pro = ip.next();
			writeString(pro.getCategory().name());
			writeString(pro.getName());
			writeString(pro.getImage());
			writeInt(pro.getDuration());
			writeBoolean(pro.isStackable());
			writeInt(pro.getCount());
			writeLong(pro.getTime());
		}
	
		
	}
	public void repRefresh(boolean b)
	{
		writeInt(Protocol.REP_REFRESH);
		writeBoolean(b);
	}
	public void repBuy(boolean b)
	{
		writeInt(Protocol.REP_BUY);
		writeBoolean(b);
	}

	public void repSell(boolean b) {
		writeInt(Protocol.REP_SELL);
		writeBoolean(b);
		
	}
	public void repConsume(boolean b) {
		
		writeInt(Protocol.REP_CONSUME);
		writeBoolean(b);
		
	}
public void repClear(boolean b) {
		
		writeInt(Protocol.REP_CLEAR);
		writeBoolean(b);
		
	}
	
}
